from OWRpy import *
import redRi18n
_ = redRi18n.get_(package = 'base')

import signals 
import redRGUI 


class addTransform(OWRpy): 
    settingsList = []
    def __init__(self, **kwargs):
        OWRpy.__init__(self, **kwargs)
        
        # Unique R variables
        self.setRvariableNames(["additive", "RlistX", "Rearranged"])
        
        # Python variables
        self.data = {}
        self.RFunctionParam_x = ''
        
        # Inputs
        self.inputs.addInput("x", "x", signals.base.RMatrix, self.processx)
        
        # Outputs
        self.outputs.addOutput("addTransform Output","addTransform Output", signals.base.RMatrix)
        
        # Select column in the list
        self.namesListX = redRGUI.base.listBox(self.controlArea, label =('Select reference element (will affect acomp and rcomp data only, but must select one)'))
        self.namesListX.setSelectionMode(QAbstractItemView.SingleSelection)
        
        self.dataClass = redRGUI.base.lineEdit(self.controlArea, label = "Data class:", text = '')
        self.dataClass.setDisabled(True)
        self.addOpLabel = redRGUI.base.lineEdit(self.controlArea, label = "Additive operation:", text = '')
        self.addOpLabel.setDisabled(True)        
        
        self.commit = redRGUI.base.commitButton(self.bottomAreaRight, _("Commit"), alignment=Qt.AlignLeft, 
        callback = self.commitFunction, processOnInput=True)
    
    def processx(self, data):
        if not self.require_librarys(["compositions"]):
            self.status.setText('R Libraries Not Loaded.')
            return
        if data:
            self.RFunctionParam_x=data.getData()
            #self.data = data
            self.namesListX.update(self.R('colnames('+self.RFunctionParam_x+')', wantType = 'list'))
            self.dataClass.setText(self.R('class('+self.RFunctionParam_x+')'))
            if self.dataClass.text() =='rplus' :
                self.addOpLabel.setText('Isometric identity transform')
                self.addOpCommand = 'iit'
            elif self.dataClass.text() =='aplus' :
                self.addOpLabel.setText('Isometric log transform')
                self.addOpCommand = 'ilt'
            elif self.dataClass.text() =='rcomp' :
                self.addOpLabel.setText('Additive planar transform')
                self.addOpCommand = 'apt'
            elif self.dataClass.text() =='acomp' :
                self.addOpLabel.setText('Additive log-ratio transform')
                self.addOpCommand = 'alr'
            else :
                self.status.setText('The class of the input is not compositional. Please parse data though widget "Close the simplex".')
                return
            self.commitFunction()
        else:
            self.RFunctionParam_x=''
    def commitFunction(self):
        if str(self.RFunctionParam_x) == '':
            self.status.setText('Data is missing.')
            return
        if self.namesListX.currentSelection() == []:
            self.status.setText('Select element.')
            return

        # Rearange data to push the selected element to the first column
        self.refColNum=self.R('grep("'+self.namesListX.currentSelection()[0]+'", names('+self.RFunctionParam_x+'))', wantType = 'list')
        self.colCount=self.R('ncol('+self.RFunctionParam_x+')', wantType = 'str')
        self.colNum=range(1,self.colCount+1)
        self.colNum.remove(self.refColNum[0])
        self.colNum.append(self.refColNum[0])
        self.R(self.Rvariables['RlistX']+'<-c(%s)' % ','.join(map(str,self.colNum)))
        self.R(self.Rvariables['Rearranged']+'<-'+self.RFunctionParam_x+'[,'+self.Rvariables['RlistX']+']', wantType='NoConversion')
        
        # Compute transformation
        self.R(self.Rvariables['additive']+'<-'+self.addOpCommand+'(x='+self.Rvariables['Rearranged']+')', processingNotice=True)
        newData = signals.base.RMatrix(self, data = self.Rvariables['additive'], checkVal = False)
        
        # Send to output signal
        self.rSend("addTransform Output", newData)